<template>

  <div>

    表单名称：<el-input v-model="formName" style="width: 240px" placeholder="请输入" />&nbsp;&nbsp;
    <el-button type="success" @click="fetchData">查询</el-button>
    <br><br>


    <el-button plain @click="dialogVisible = true, resetForm(ruleFormRef)">新增 </el-button>


    <br>
    <br>
    <!--显示列表-->
    <el-table :data="tableData" class="table">
      <el-table-column type="index" label="序号" width="60" align="center" :index="indexMethod" />
      <el-table-column prop="formName" label="表单名称" style="width: 250px;" />
      <el-table-column prop="formDescribe" label="表单描述" style="width: 250px;" />
    </el-table>
    <!--分页-->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"
      :page-sizes="[15, 20, 25, 30]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount" class="pagination"></el-pagination>



    <!--弹出框新增-->
    <el-dialog v-model="dialogVisible" title="新增表单" width="500">
      <span>

        <el-form ref="ruleFormRef" style="max-width: 600px" :model="RuleForm" :rules="rules" label-width="auto"
          class="demo-ruleForm" status-icon>
          <el-form-item label="表单名称" prop="formName">
            <el-input v-model="RuleForm.formName" />
          </el-form-item>

          <el-form-item label="表单描述" prop="formDescribe">
            <el-input v-model="RuleForm.formDescribe" />
          </el-form-item>

          <el-form-item label="选择行业" prop="selectIndustry">
            <el-select v-model="RuleForm.selectIndustry" placeholder="请选择行业">
              <el-option v-for="option in options" :key="option.value" :label="option.txt" :value="option.value" />
            </el-select>
          </el-form-item>

          <el-form-item label="选择大类" prop="selectiveCategory">
            <el-select v-model="RuleForm.selectiveCategory" placeholder="请选择大类">
              <el-option label="事件" value="事件" />
            </el-select>
          </el-form-item>
          <el-form-item label="选择小类" prop="selectiveSubclass">
        <el-select v-model="RuleForm.selectiveSubclass" placeholder="请选择小类">
            <el-option v-for="option in options2" :key="option.value" :label="option.label" :value="option.value" />
          </el-select>
      </el-form-item>


          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              新增
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
          </el-form-item>
        </el-form>
      </span>
    </el-dialog>
  </div>

</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import axios from 'axios'
//关于弹出框
const dialogVisible = ref(false)
//关于分页
const tableData = ref([])
const pageIndex = ref(1)
const pageSize = ref(15)
const totalCount = ref(0)
// 序号
const indexMethod = (index: number) => {
  return index += (pageSize.value) * (pageIndex.value - 1) + 1;
}

//行业下拉框
interface Ioption {
  txt: string
  value: number
}

//行业下拉框数组
const options = ref<Ioption[]>([])


interface Ioption{
  label:string,
  value:number,
}
const options2 = ref<Ioption[]>([])
//查询的表单名称输入框
const formName = ref('')
//显示列表
const fetchData = async () => {
  try {
    const response = await axios.get('http://47.95.178.186:8000/IncidentGetRout/api/FormManagementRead/ShowFormManagement', {
      params: {
        PageIndex: pageIndex.value,
        PageSize: pageSize.value,
        formName: formName.value
      }
    })

    tableData.value = response.data.data
    totalCount.value = response.data.totalCount
  } catch (error) {
    console.error(error)
  }
}
//分页
const handleSizeChange = (val: number) => {
  pageSize.value = val
  fetchData()
}
//分页
const handleCurrentChange = (val: number) => {
  pageIndex.value = val
  fetchData()
}
//绑定行业下拉框
const fetchOptions = async () => {
  try {
    const response = await axios.get('http://47.95.178.186:8000/IncidentGetRout/api/FormManagementRead/zhuanSheepBreed')
    // 将后端返回的选项数据保存到 options 中
    options.value = response.data
  } catch (error) {
    console.error('获取下拉框选项失败:', error)
  }
}
//绑定小类下拉框
const BindSubclasses = async () => {
  try {
    const response = await axios.get('http://47.95.178.186:8000/GetLiGriddingRout/api/AssemType/ShowTreeInfo')
    // 将后端返回的选项数据保存到 options 中
    options2.value = response.data
  } catch (error) {
    console.error('获取下拉框选项失败:', error)
  }
}
//获取表单中的信息
const RuleForm = reactive({
  formName: '',
  formDescribe: '',
  selectIndustry: '',
  selectiveCategory: '',
  selectiveSubclass: '',
})
const ruleFormRef = ref<FormInstance>()

//验证
const rules = reactive<FormRules>({
  formName: [
    { required: true, message: '表单名称不能为空', trigger: 'blur' },
  ],
  formDescribe: [
    { required: true, message: '表单描述不能为空', trigger: 'blur' },
  ],
  selectIndustry: [
    { required: true, message: '选择行业不能为空', trigger: 'change' },
  ],
  selectiveCategory: [
    { required: true, message: '大类不能为空', trigger: 'change' },
  ],
  selectiveSubclass: [
    { required: true, message: '小类不能为空', trigger: 'change' },
  ],
})
//新增功能
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      axios.post('http://47.95.178.186:8000/IncidentSetRout/api/FormManagement/CreateFormManagement', RuleForm)
        .then(response => {
          if (response.data.code === 200) {
            dialogVisible.value = false
            fetchData()
            ElMessage.success(response.data.msg)
          } else {
            // 处理新增失败情况
            ElMessage.error(response.data.msg)
          }
        })

    } else {
      console.log('error submit!', fields)
    }
  })
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
interface Tree {
  [key: string]: any
}

onMounted(() => {
  //调用行业下拉框方法
  fetchOptions();
  BindSubclasses();
  //调用显示方法
  fetchData();
})


</script>
